package com.aos.eap.adapter;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.aos.eap.R;
import com.aos.eap.bean.CheckBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * @author
 * @date 2017/9/11
 * @desc
 */

public class CheckListAdapter extends BaseQuickAdapter<CheckBean, BaseViewHolder> {

    public CheckListAdapter(@Nullable List<CheckBean> data) {
        super(R.layout.adapter_check_list, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, final CheckBean item) {
        helper.setText(R.id.tvTitle, item.getItem());
        helper.setText(R.id.tvNum,String.valueOf(helper.getAdapterPosition() + 1));
        EditText editText = helper.getView(R.id.etContent);
        LinearLayout llCheck = helper.getView(R.id.llCheck);
        switch (item.getItemType()) {
            case "1":
                editText.setText(item.getItemValue());
                editText.setVisibility(View.VISIBLE);
                llCheck.setVisibility(View.GONE);
                editText.setInputType(InputType.TYPE_CLASS_TEXT);
                setEditListener(editText, item);
                break;
            case "2":
                editText.setText(item.getItemValue());
                editText.setVisibility(View.VISIBLE);
                llCheck.setVisibility(View.GONE);
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                setEditListener(editText, item);
                break;
            case "3":
                editText.setVisibility(View.GONE);
                llCheck.setVisibility(View.VISIBLE);
                final RadioButton rbOK = helper.getView(R.id.rbOK);
                final RadioButton rbNG = helper.getView(R.id.rbNG);
                switch (item.getItemValue()) {
                    case "OK":
                        rbOK.setChecked(true);
                        rbNG.setChecked(false);
                        break;
                    case "NG":
                        rbOK.setChecked(false);
                        rbNG.setChecked(true);
                        break;
                    default:
                        rbOK.setChecked(false);
                        rbNG.setChecked(false);
                        break;
                }
                rbOK.setOnClickListener(v -> item.setItemValue("OK"));
                rbNG.setOnClickListener(v -> item.setItemValue("NG"));
                break;
        }
    }

    private void setEditListener(EditText editText, final CheckBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setItemValue(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
